import sql from './createTable.sql?raw'
// 导入接口代码
import request from '../api'

let mainLevels = [] as LevelType[]
let mainLevelsTotal = 0
let mainPassNum = 0
let customLevels = [] as LevelType[]
let customLevelsTotal = 0
let customPassNum = 0
let levelList = []

// 自定义关卡sql
import sql1 from './custom/大浪淘鸡/createTable.sql?raw'
import sql2 from './custom/冒险者和金币/createTable.sql?raw'
import sql3 from './custom/魔法学院/createTable.sql?raw'
let customSQL = [sql1, sql2, sql3]

// 获取所有关卡
await request.level
  .getAllLevel({ pageNum: 1, pageSize: 999999 })
  .then((res: any) => {
    levelList = res.data.list
    let mlevelNum = 1
    let cLevelNum = 1
    let customSQLNum = 0
    for (let i = 0; i < levelList.length; i++) {
      if (levelList[i].typeName === 'main') {
        mainLevels.push({
          id: levelList[i].id,
          key: 'mlevel' + mlevelNum++,
          title: levelList[i].title,
          initSQL: sql,
          content:
            '# ' +
            levelList[i].title +
            '\n## 教程\n' +
            levelList[i].tutorial +
            '\n## 示例\n' +
            levelList[i].example +
            '\n## 题目\n' +
            levelList[i].topic,
          defaultSQL: 'select * from student;',
          answer: levelList[i].rfanswer,
          hint: '请仔细查看本关给出的示例',
          type: levelList[i].typeName,
          rate: levelList[i].rate,
          status: levelList[i].status
        } as LevelType)
        if (levelList[i].status === 1) {
          ++mainPassNum
        }
      } else if (levelList[i].typeName === 'custom') {
        customLevels.push({
          id: levelList[i].id,
          key: 'clevel' + cLevelNum++,
          title: levelList[i].title,
          initSQL: customSQL[customSQLNum++],
          content:
            '# ' +
            levelList[i].title +
            '\n## 教程\n' +
            levelList[i].tutorial +
            '\n## 示例\n' +
            levelList[i].example +
            '\n## 题目\n' +
            levelList[i].topic,
          defaultSQL: 'select * from student;',
          answer: levelList[i].rfanswer,
          hint: '请仔细查看本关给出的示例',
          type: levelList[i].typeName,
          rate: levelList[i].rate,
          status: levelList[i].status
        } as LevelType)
        if (levelList[i].status === 1) {
          ++customPassNum
        }
      }
    }
    mainLevelsTotal = mainLevels.length
    customLevelsTotal = customLevels.length
  })

export default {
  mainLevels,
  customLevels,
  mainLevelsTotal,
  customLevelsTotal,
  mainPassNum,
  customPassNum
}
